VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Obj"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Const MAXCLASSES = 6
Function OverEnrolled(conn As ADODB.Connection, lngStudentID As Long) As Boolean
    Dim rsEnrollment As Object
    Dim strSQL As String
    
    'Student cannot add if already at class limit
    strSQL = "select count(studentid) from enrollment where studentid = " & lngStudentID
    Set rsEnrollment = CreateObject("ADODB.recordset")
    rsEnrollment.ActiveConnection = conn
    rsEnrollment.Open strSQL, , adOpenKeyset
    If rsEnrollment.Fields(0).Value > MAXCLASSES Then
        OverEnrolled = True
    Else
        OverEnrolled = False
    End If
    rsEnrollment.Close
End Function
Public Function Add(ByVal lngStudentID As Long, ByVal strClassID As String) As Integer
    Dim ctxObject As ObjectContext
    Dim conn As ADODB.Connection
    Dim strSQL As String
    
    On Error GoTo ErrorHandler
    Add = 0
    'Get Context Object
    Set ctxObject = GetObjectContext
    Set conn = CreateObject("ADODB.Connection")

    'Open connection with State University Database
    conn.Open "DSN=StateU;UID=sa;PWD=;"

    'Business rule
    'See if student is over enrolled
    If OverEnrolled(conn, lngStudentID) Then
        Add = 1
        ctxObject.SetAbort
        Exit Function
    End If
    
    'Perform the work of adding the student to the class
    strSQL = "INSERT INTO enrollment (ClassID, StudentID) VALUES ('" & strClassID & "'," & lngStudentID & ")"
    conn.Execute strSQL
        
    'Close the connection
    conn.Close
    'Work completed successfully
    ctxObject.SetComplete
    Exit Function

ErrorHandler:
    'Just abort if any errors occur
    Add = 1
    ctxObject.SetAbort
End Function

